#include<iostream>
#include<set>
using namespace std;
// zdl:: 这道题目其实就是考察一个并查集和图的性质
int n, m;
const int N = 1e3 + 10;
int fa[N];

int find(int x)
{
    return x == fa[x] ? fa[x] : fa[x] = find(fa[x]);
}
void un(int x, int y)
{
    fa[find(x)] = find(y); 
}

int main()
{
    while (cin >> n, n != 0)
    {
        cin >> m;
        // 初始化并查集
        for (int i = 1; i <= n; i++) fa[i] = i;
        while (m--)
        {
            int x, y; cin >> x >> y;
            un(x, y);
        }
        // zdl:: 接下来就是查找连通块的数量
        set<int> s;
        for (int i = 1; i <= n; i++)
        {
            s.insert(find(i));
        }
        cout << s.size() - 1 << endl;
    }
    return 0;
}